package combine;

import org.junit.Test;

/**
 * =================================
 * <p>
 * Created by cjj on 18-10-15.
 * <p>
 * 描述:
 */

public class Combine {

    public static final int N = 3;
    public static final int M = 100;

    /**
     *
     *
     */
    @Test
    public void combine() {
        int i, j;

        int[] b = new int[M];

        for (i = 0; i < N; i++) {
            b[i] = 1;
        }
        int flag[] = new int[M];
        int max = N;
        for (i = 0; i < max; i++) {
            if (b[i] == 1) {
                System.out.print((i + 1) + " ");
            }
        }
        System.out.println();
        do {
            for (i = 0; i < M - 1; i++) {
                if (b[i] == 1 && b[i + 1] == 0) {
                    b[i] = 0;
                    b[i + 1] = 1;

                    //让最左边的数为1,不够补0
                    if (i >= N && flag[i + 1] == 0) {
                        for (j = 0; j < i; j++) {
                            if (j < N - 1) {
                                b[j] = 1;
                            } else {
                                b[j] = 0;
                            }
                        }
                        flag[i + 1] = 1;

                    }
                    if (max < (i + 1)) {
                        max = i + 1;
                    }
                    for (j = 0; j <= max; j++) {
                        if (b[j] == 1) {
                            System.out.print((j + 1) + " ");
                        }
                    }
                    System.out.println();
                    break;
                }
            }

        } while (isFinish(b));
    }

    public boolean isFinish(int[] b) {
        for (int i = M - 1; i >= M - N; i--) {
            if (b[i] != 1) {
                return true;
            }
        }
        return false;
    }



}

